package net.jjjshop.front.controller.lbt;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.jjjshop.common.entity.lbt.JybUserPaytypeEntity;
import net.jjjshop.common.service.lbt.JybUserPaytypeService;
import net.jjjshop.common.service.user.UserService;
import net.jjjshop.framework.common.api.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 用户交易支付方式 控制器

 */
@RestController
@RequestMapping("/front/jybuserpaytype")
@Api(tags = "用户交易支付方式")
public class JybUserPaytypeController {
    @Autowired
    private JybUserPaytypeService jybUserPaytypeService;
    @Autowired
    private UserService userService;
    /**
     * 列表信息
     */
    @ApiOperation(value = "列表信息")
    @PostMapping("/list")
    public ApiResult<IPage<JybUserPaytypeEntity>> list(Integer page, Integer limit){
        return ApiResult.ok(jybUserPaytypeService.getList(1,10));
    }


    /**
     * 详情数据
     */
    @ApiOperation(value = "详情数据")
    @PostMapping("/info/{id}")
    public ApiResult<JybUserPaytypeEntity> info(@PathVariable("id") Integer id){
		JybUserPaytypeEntity jybUserPaytype = jybUserPaytypeService.getById(id);

        return ApiResult.ok(jybUserPaytype);
    }

    /**
     * 新增数据
     */
    @ApiOperation(value = "新增数据")
    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody List<JybUserPaytypeEntity> list){
        if (null == list){
            return ApiResult.fail("请提交数据");
        }

        for (JybUserPaytypeEntity bean : list){
            if (StringUtils.isEmpty(bean.getName())){
                return ApiResult.fail("姓名不能为空");
            }
            if (StringUtils.isEmpty(bean.getAccount())){
                return ApiResult.fail("账号不能为空");
            }
            if (null ==bean.getType()){
                return ApiResult.fail("付款方式不能为空");
            }
            bean.setCreateTime(new Date());
            bean.setUid(userService.getUserId());
        }
        if (jybUserPaytypeService.saveBatch(list)) {
            return ApiResult.ok("成功");
        }
        return ApiResult.fail("失败");
    }

    /**
     * 修改数据
     */
    @ApiOperation(value = "修改数据")
    @PostMapping("/update")
    public ApiResult<String> update(@RequestBody List<JybUserPaytypeEntity> list){
        if (jybUserPaytypeService.updateBatchById(list)) {
            return ApiResult.ok("更新成功");
        }
        return ApiResult.fail("修改失败");
    }

    /**
     * 删除:根据id集合
     */
    @ApiOperation(value = "删除:根据id集合")
    @PostMapping("/delete")
    public ApiResult<String> delete(@RequestBody Integer[] ids){
        if (jybUserPaytypeService.removeByIds(Arrays.asList(ids))) {
            return ApiResult.ok("操作成功");
        }
        return ApiResult.fail("操作失败");
    }

}
